package ylyhappy.cn.ideavim.ui.actionpreview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.card.MaterialCardView;

import java.util.List;

import ylyhappy.cn.ideavim.R;
import ylyhappy.cn.ideavim.lg.db.Action;

public class ActionListAdapter extends RecyclerView.Adapter<ActionListAdapter.MyViewHolder> {


    private final List<Action> mList;
    private final Context mContext;
    private OnItemClickListener mOnItemClickListener;

    public ActionListAdapter(List<Action> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void removeData(int position) {
        mList.remove(position);
        notifyItemRemoved(position);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.action_recycler_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.en.setText(mList.get(position).getEn());
        holder.zh.setText(mList.get(position).getZh());
        if (mOnItemClickListener != null) {
            holder.container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.container, pos);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView en;
        TextView zh;
        MaterialCardView container;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            en = itemView.findViewById(R.id.en);
            zh = itemView.findViewById(R.id.zh);
            container = itemView.findViewById(R.id.action_recycler_list_item_card);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
//        void onItemLongClick(View view, int position);
    }

}
